﻿using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.EntityFrameworkCore;
using Rj_GsInsurance.Client.Extensions;
using Rj_GsInsurance.Server.DataBaseContext;
using Rj_GsInsurance.Server.DataBaseContext.Entities;
using Rj_GsInsurance.Server.Extensions;
using Rj_GsInsurance.Server.Models;
using Rj_GsInsurance.Server.Models.InsurMsgDataInput;
using Rj_GsInsurance.Server.Models.InsurMsgDataOutput;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace Rj_GsInsurance.Client.ViewModels
{
    public partial class ApprovalManageViewModel : ObservableObject
    {
        [ObservableProperty]
        private ApprovalUploadDto _ApprovalUploadDto;
        [ObservableProperty]
        private ObservableCollection<ApprovalManageFzqjViewModel> _ApprovalFzqjViewModel;
        [ObservableProperty]
        private ObservableCollection<InsurWorkInjuryAuxiliaryEquipment> _AuxiliaryEquipments;
        [ObservableProperty]
        private InsurWorkInjuryAuxiliaryEquipment _SelectedAuxiliaryEquipment;
        [ObservableProperty]
        private ObservableCollection<DiagViewModel> _DiagViewModel;
        [ObservableProperty]
        private DiagViewModel _SelectedDiagViewModel;       
        
        [ObservableProperty]
        private ObservableCollection<QueryApprovalViewModel> _queryApprovalViewModels;
        [ObservableProperty]
        private QueryApprovalViewModel _SelectedQueryApprovalViewModel;
        [ObservableProperty]
        private ObservableCollection<ReadCardOutput> _ReadCardInfoList;
        private ReadCardOutput _SelectedReadCardInfo;
        public ReadCardOutput SelectedReadCardInfo
        {
            get => _SelectedReadCardInfo;
            set
            {
                _ = SetProperty(ref _SelectedReadCardInfo, value);
                if (value != null)
                {
                    ApprovalUploadDto.Grbh = value.psn_no;
                    ApprovalUploadDto.Rysstcq = value.insuplc_admdvs;
                    ApprovalUploadDto.Dyryid = value.salaryId;
                }
            }
        }
        private string _SearchBzValue;
        public string SearchBzValue
        {
            get { return _SearchBzValue; }
            set
            {
                if( _SearchBzValue != value)
                {
                    _ = SetProperty(ref _SearchBzValue, value);
                    OnSearchBz(value);
                }
            }
        }
        //[ObservableProperty]
        //private ObservableCollection<DiagViewModel> _DiagnosisViewModel;
        [ObservableProperty]
        private DateTime? _BeginTime;
        [ObservableProperty]
        private DateTime? _EndTime;
        [ObservableProperty]
        private DateTime? _RdTime;
        [ObservableProperty]
        private DateTime? _LdjdTime;
        [ObservableProperty]
        private Visibility _ChoiceItemVisibility=Visibility.Collapsed;

        private InsurTypeCode _splbSelected;

        public InsurTypeCode SplbSelected
        {
            get { return _splbSelected; }
            set
            {
                if (_splbSelected != value)
                {
                    _ = SetProperty(ref _splbSelected, value);
                    ApprovalUploadDto.Splb= _splbSelected.Code;
                }
            }
        }
        [ObservableProperty]
        private InsurTypeCode _SelectedYllb;
        [ObservableProperty]
        private ObservableCollection<InsurTypeCode> _splbList = new ObservableCollection<InsurTypeCode>()
        {
            new InsurTypeCode("20","工伤康复门诊审批"),
            new InsurTypeCode("21","工伤康复住院审批"),
            new InsurTypeCode("22","工伤旧伤复发治疗审批"),
            new InsurTypeCode("23","首次配置辅助器具审批"),
            new InsurTypeCode("26","老工伤医疗费审批"),
            new InsurTypeCode("28","更换辅助器具审批"),
            new InsurTypeCode("29","工伤二次治疗审批"),
        };

        [ObservableProperty]
        private List<InsurTypeCode> _yllb = new List<InsurTypeCode>()
        {
            new InsurTypeCode ("41","门诊"),
            new InsurTypeCode ("45","住院"),
            new InsurTypeCode ("99","全部"),
        };
        [ObservableProperty]
        private List<InsurTypeCode> _existsControversy = new List<InsurTypeCode>()
        {
            new InsurTypeCode ("0","否"),
            new InsurTypeCode ("1","是"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedExistsControversy;
        [ObservableProperty]
        private List<InsurTypeCode> _Jycslx = new List<InsurTypeCode>()
        {
            new InsurTypeCode ("0","多次"),
            new InsurTypeCode ("1","单次"),
        };
        [ObservableProperty]
        private InsurTypeCode _selectedJycslx;

        public HisDbContext HisDbContext { private set; get; }
        public ISnackbarService SnackbarService { get; set; }
        public IMapper Mapper { get; set; }

        public ApprovalManageViewModel(ApprovalUploadDto approvalUploadDto, HisDbContext hisDbContext, ISnackbarService snackbarService, IMapper mapper)
        {
            ApprovalUploadDto = approvalUploadDto;
            HisDbContext = hisDbContext;
            SnackbarService = snackbarService;
            Mapper = mapper;
        }
        [RelayCommand]
        private void OnAddChoiceFzqj(object prama)
        {
            ChoiceItemVisibility = Visibility.Visible;
        }
        [RelayCommand]
        private void OnChoiceFzqj(object prama)
        {
            if (SelectedAuxiliaryEquipment == null)
            {
                SnackbarService.Show(
                        "警告!",
                        $"请选择辅助器具",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }
            ApprovalFzqjViewModel.Add(new ApprovalManageFzqjViewModel() { Fzqjbm=this.SelectedAuxiliaryEquipment.Fzqjbm,Fzqjmc=this.SelectedAuxiliaryEquipment.Fzqjmc,Zdsynx=this.SelectedAuxiliaryEquipment.Zdsynx,Zgzfxe=this.SelectedAuxiliaryEquipment.Zgzflj});
            ChoiceItemVisibility = Visibility.Collapsed;
        }

        [RelayCommand]
        private void OnCancelChoiceFzqj(object prama)
        {
            this.SelectedAuxiliaryEquipment = null;
            ChoiceItemVisibility = Visibility.Collapsed;
        }

        [RelayCommand]
        private async Task OnSearchBz(object prama)
        {   
            if (_SearchBzValue == this.SelectedDiagViewModel?.Bzmc)
                return;
            if(string.IsNullOrWhiteSpace(_SearchBzValue))
            {
                //var searchResult = await HisDbContext.InsurWorkInjuryDiseaseCatalogues.Take(100).ToListAsync();
                //DiagViewModel = new ObservableCollection<DiagViewModel>(searchResult.Select(s => new DiagViewModel() { Bzbm = s.Jbbm, Bzmc = s.Jbmc }));
            }
            else
            {
                var searchResult = await HisDbContext.InsurWorkInjuryDiseaseCatalogues.Where(w => w.Jbmc.Contains(_SearchBzValue)).Take(100).ToListAsync();
                DiagViewModel = new ObservableCollection<DiagViewModel>(searchResult.Select(s => new DiagViewModel() { Bzbm = s.Jbbm, Bzmc = s.Jbmc }));
            }
            
        }
        [RelayCommand]
        private void OnReadCard(object param)
        {
            ReadCardInput input=new ReadCardInput();
            try
            {
                var insurMsgOut = input.Excute<ReadCardInput, List<ReadCardOutput>>("2100");
                if (insurMsgOut?.output?.Count > 0)
                {
                    ReadCardInfoList = new ObservableCollection<ReadCardOutput>(insurMsgOut.output);
                }
                //var output = insurMsgOut.output.FirstOrDefault();
                //if (output != null)
                //{
                //    ApprovalUploadDto.Grbh = output.psn_no;
                //    ApprovalUploadDto.Rysstcq = output.insuplc_admdvs;
                //    ApprovalUploadDto.Dyryid = output.psn_no;
                //}
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                        "警告!",
                        $"读卡失败!{ex.Message}",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }
        }
        [RelayCommand]
        private void OnUpload(object param)
        {
            ApprovalUploadInput input = new ApprovalUploadInput();
            input.Grbh= ApprovalUploadDto?.Grbh;
            input.Splb= ApprovalUploadDto?.Splb;
            input.Jzlsh= ApprovalUploadDto?.Jzlsh;
            input.Rysstcq=ApprovalUploadDto?.Rysstcq;
            input.Bzbm=SelectedDiagViewModel?.Bzbm;
            input.Bzmc=SelectedDiagViewModel?.Bzmc;
            input.Xyjgyj = ApprovalUploadDto?.Jgyj;
            input.By1 = string.Empty;
            input.By2 = string.Empty;
            input.Jzyllb = ApprovalUploadDto?.Jzyllb;
            input.Jzyllb = SelectedYllb?.Code;
            input.Sbrq = DateTime.Now.ToString("yyyyMMddHHmmss");
            input.Kssj = BeginTime?.ToString("yyyyMMddHHmmss");
            input.Zzsj = EndTime?.ToString("yyyyMMddHHmmss");
            input.Jbr = "管理员";
            input.Sfczzy = SelectedExistsControversy?.Code;
            input.Bz= ApprovalUploadDto?.Memo;
            input.Zrxyjgbm=string.Empty;
            input.Zrxyjgmc=string.Empty;
            input.Gsrdsj= RdTime?.ToString("yyyyMMddHHmmss");
            input.Ldjdsj= LdjdTime?.ToString("yyyyMMddHHmmss");
            input.Zd= ApprovalUploadDto?.Zd;
            input.Eczlly= ApprovalUploadDto?.Eczlly;
            input.Xyjgybbyj= ApprovalUploadDto?.Ybbyj;
            input.Shbw= ApprovalUploadDto?.Shbw;
            input.Lwjsbz = "1";
            input.Dyryid= ApprovalUploadDto?.Dyryid;
            input.Jycslx = SelectedJycslx?.Code;
            input.Fzqjlb = ApprovalFzqjViewModel?.ToInputString();

            try
            {
                var output = input.Excute<ApprovalUploadInput, ApprovalUploadOutput>("3110");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                        "警告!",
                        $"上报失败!{ex.Message}",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }

            SnackbarService.Show(
                        "提示!",
                        $"上报成功!",
                        ControlAppearance.Success,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
        }
        [RelayCommand]
        private void OnQuery(object param)
        {
            try
            {
                QueryApprovalInput input = new QueryApprovalInput();
                var insurMsgOut = input.Excute<QueryApprovalInput, List<QueryApprovalOutput>>("1600");
                QueryApprovalViewModels = Mapper.Map<List<QueryApprovalOutput>, ObservableCollection<QueryApprovalViewModel>>(insurMsgOut.output);
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                        "警告!",
                        $"查询失败!{ex.Message}",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }
        }
        [RelayCommand]
        private void OnCancelUpload(object param)
        {
            if (SelectedQueryApprovalViewModel == null)
            {
                SnackbarService.Show(
                        "警告!",
                        $"请选择需要撤销的记录!",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }

            try
            {
                ApprovalUploadCancelInput input=new ApprovalUploadCancelInput();
                input.Splb= SelectedQueryApprovalViewModel.Splb;
                input.Spbh= SelectedQueryApprovalViewModel.Spbh;
                input.Jzlsh= string.Empty;
                input.Bzbm= SelectedQueryApprovalViewModel.Bzbm;
                input.Fzqjbm= string.Empty;
                input.Kssj= SelectedQueryApprovalViewModel.Kssj;
                var output = input.Excute<ApprovalUploadCancelInput, ApprovalUploadCancelOutput>("3120");
            }
            catch (Exception ex)
            {
                SnackbarService.Show(
                        "警告!",
                        $"撤销失败!{ex.Message}",
                        ControlAppearance.Danger,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
                return;
            }

            SnackbarService.Show(
                        "提示!",
                        $"撤销成功!",
                        ControlAppearance.Success,
                        new SymbolIcon(SymbolRegular.Fluent24),
                        TimeSpan.FromSeconds(30)
                    );
        }
    }
}
